package com.yuerlive.rainbow.module.main.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;
import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseFragment;
import com.yuerlive.rainbow.controller.LiveRoomController;
import com.yuerlive.rainbow.entity.LiveInfo;
import com.yuerlive.rainbow.entity.ObjectWrapper;
import com.yuerlive.rainbow.module.homepage.HomePageActivity;
import com.yuerlive.rainbow.module.live.LiveRoomActivity;
import com.yuerlive.rainbow.module.main.adapter.MainFollowedAdapter;
import com.yuerlive.rainbow.module.main.mainInterface.MainFragmentListener;
import com.yuerlive.rainbow.module.main.presenter.HomeContract;
import com.yuerlive.rainbow.module.main.presenter.HomePresenter;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.widget.CustomDialog;
import com.yuerlive.rainbow.widget.LinearLayoutManagerWrapper;

import java.util.ArrayList;
import java.util.IllegalFormatCodePointException;
import java.util.List;

import butterknife.BindView;

/**
 * Created by xiaoyi on 2017/5/26.
 */

public class FragmentFollowed extends BaseFragment implements HomeContract.View {
    @BindView(R.id.refresh)
    PullLoadMoreRecyclerView recycleView;
    MainFollowedAdapter adapter;
    List<ObjectWrapper> mDatas = new ArrayList<>();
    HomeContract.Presenter presenter;
    boolean isLoading;
    private MainFragmentListener topListener;
    private MainFragmentListener buttomListener;
    private static final int SCROLL_DISTANCE = 35;
    private int totalScrollDistance;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycle;
    }

    @Override
    public void initViews(View root) {
        presenter = new HomePresenter(this);
        presenter.getFollowed(false);
        recycleView.setColorSchemeResources(R.color.colorPrimary);
        LinearLayoutManagerWrapper linearLayoutManager = new LinearLayoutManagerWrapper(mActivity, LinearLayoutManager.VERTICAL, false);

        recycleView.getRecyclerView().setLayoutManager(linearLayoutManager);
        recycleView.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                presenter.getFollowed(false);
            }

            @Override
            public void onLoadMore() {
                if (!isLoading) {
                    isLoading = true;
                    presenter.getFollowed(true);
                }
            }
        });
        recycleView.setRefreshing(true);
        recycleView.getRecyclerView().setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    int topReal = ((FragmentMain) getParentFragment()).getTobarRealHeight();
                    int topTransY = ((FragmentMain) getParentFragment()).getTopTranslationY();
                    if (Math.abs(topTransY) >= topReal / 2) {
                        recycleView.getRecyclerView().smoothScrollBy(0, topReal - Math.abs(topTransY));
                        ((FragmentMain) getParentFragment()).doTopTranslate(topReal);
                        return false;
                    } else if (Math.abs(topTransY) < topReal / 2) {
                        recycleView.getRecyclerView().smoothScrollBy(0, topTransY);
                        ((FragmentMain) getParentFragment()).doTopTranslate(0);
                        return false;
                    }
                    break;
            }
            return false;
        });
        adapter = new MainFollowedAdapter(mActivity, mDatas);
        adapter.setOnItemClickListener((v, p) -> {
            Intent i = new Intent();
            LiveInfo info = (LiveInfo) mDatas.get(p).object;
            if (info.getState() != 0) {
                i.setClass(mActivity, LiveRoomActivity.class);
                i.putExtra("liveId", info.getLiveId());
                i.putExtra("chatroomId", info.getChatRoomId());
                i.putExtra("liveCover", info.getLiveIcon());
                LiveRoomController.getInstance().enterLiveRoom(mActivity, i);
            } else {
                i.setClass(mActivity, HomePageActivity.class);
                i.putExtra("otherId", info.getUserId());
                jumpToActivity(i);
            }

        });
        recycleView.setAdapter(adapter);
    }

    public void setFragmentMainListener(MainFragmentListener listener) {
        topListener = listener;
    }

    @Override
    public void setPresenter(HomeContract.Presenter presenter) {
        super.presenter = presenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        isLoading = false;
        recycleView.setPullLoadMoreCompleted();
    }

    @Override
    public void showFullData(List<ObjectWrapper> results) {
        //不会被调用,关注列表没有banner
        L.e(TAG, "results \n" + results);

    }

    @Override
    public void showLiveData(List<ObjectWrapper> results, int page) {
        L.e(TAG, "results \n" + results);
        if (page == 1) {
            mDatas.clear();
            mDatas.add(new ObjectWrapper(MainFollowedAdapter.TYPE_HEADER, null));
        }
        if (results != null && results.size() != 0) {
            mDatas.addAll(results);
        } else {
//            showToast("已经到底啦");
        }
        adapter.notifyDataSetChanged();

    }

    @Override
    public void showMoreData(List<ObjectWrapper> results) {

    }

    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof MainFragmentListener)
            buttomListener = (MainFragmentListener) context;
    }
}
